import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_pdfview/flutter_pdfview.dart';

import '../../../util/ImConstant.dart';

class PdfViewPage extends StatefulWidget {

  final String title;
  final String filePath;

  const PdfViewPage({ required this.title, required this.filePath, super.key});

  @override
  State<StatefulWidget> createState() => _PdfViewState();

}

class _PdfViewState extends State<PdfViewPage> {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: _buildAppBar(context),
        backgroundColor: ImConstant.IM_MAIN_BACKGROUND_COLOR,
        body: PDFView(
          filePath: widget.filePath,
          enableSwipe: true,
          swipeHorizontal: false,
          autoSpacing: false,
          pageFling: false,
          onRender: (_pages) {

          },
          onError: (error) {
            print(error.toString());
          },
          onPageError: (page, error) {
            print('$page: ${error.toString()}');
          },
          onViewCreated: (PDFViewController pdfViewController) {
            // _controller.complete(pdfViewController);
          },
          onPageChanged: (int? page, int? total) {
            print('page change: $page/$total');
          },
        )
    );
  }

  AppBar _buildAppBar(BuildContext context) {
    return AppBar(
      backgroundColor: ImConstant.IM_MAIN_BACKGROUND_COLOR,
      leading: GestureDetector(
        child: const Icon(Icons.chevron_left, color: Colors.black),
        onTap: () => Navigator.of(context).pop(),
      ),
      centerTitle: true,
      title: Text(
        widget.title,
        style: const TextStyle(color: Colors.black, fontSize: ImConstant.IM_DEFAULT_SIZE),
      ),
    );
  }

}